package stud.aistar.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import stud.aistar.entity.CodeStatus;
import stud.aistar.entity.Result;
import stud.aistar.entity.Treatment;
import stud.aistar.service.IDoctorPatientVoService;
import stud.aistar.service.IRegOfficeService;
import stud.aistar.service.ITreatmentService;
import stud.aistar.util.DateUtil;
import stud.aistar.util.LayuiUtil;
import stud.aistar.vo.DoctorPatientVo;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * *
 *
 * @ClassName :wangyu
 * @Description
 * @Author
 * @Date2019/9/29 21:35
 * @Version V1.0
 */
@Controller
public class DoctorPatientController {

    @Autowired
    private IDoctorPatientVoService doctorPatientVoService;

    @Autowired
    private ITreatmentService treatmentService;

    @Autowired
    private IRegOfficeService regOfficeService;

    @RequestMapping("/view")
    @ResponseBody
    private Object viewJson(HttpServletRequest req){

        String docId = req.getParameter("docId");

        List<DoctorPatientVo> voList = doctorPatientVoService.findAll(1);

        Iterator<DoctorPatientVo> iterator = voList.iterator();

        while (iterator.hasNext()){
            DoctorPatientVo doctorPatientVo = iterator.next();
            if (!doctorPatientVo.getDoctorId().equals(Integer.valueOf(docId))){
                    iterator.remove();
            }
        }
        Integer count = doctorPatientVoService.findCount();
        return new LayuiUtil<DoctorPatientVo>(0,"ok",count,voList);
    }

    @RequestMapping("/patientPageHelper")
    @ResponseBody
    public Object patientPageHelper(@RequestParam String docId,
                                @RequestParam Map<String,String> paramMap){

        PageInfo<DoctorPatientVo> pageInfo = doctorPatientVoService.findPage(Integer.valueOf(docId),paramMap);
        Map<String,Object> map = new HashMap<>();
        map.put("data",pageInfo.getList());
        map.put("code",0);
        map.put("msg","请求成功");
        map.put("count",pageInfo.getTotal());
        return map;
    }

    @PostMapping("/insertValue")
    @ResponseBody
    public Result insertTreatment(HttpServletRequest req) {

        String patientName = req.getParameter("patientName");
        String patientAge = req.getParameter("patientAge");
        String patientSex = req.getParameter("patientSex");
        String personCard = req.getParameter("personCard");
        String docName = req.getParameter("docName");
        String sdeptName = req.getParameter("sdeptName");
        String intro = req.getParameter("intro");
        String doctorId = req.getParameter("doctorId");
        String patId = req.getParameter("patId");

        Treatment treatment = new Treatment();
        treatment.setPatName(patientName);
        treatment.setPatAge(patientAge);
        treatment.setPatSex(patientSex);
        treatment.setPersonCard(personCard);
        treatment.setDocName(docName);
        treatment.setConName(sdeptName);
        treatment.setDocType("1");
        treatment.setIntro(intro);
        treatment.setTreStatus(0);
        treatment.setCreateTime(DateUtil.getCurrentTime());
        treatment.setDocId(Integer.valueOf(doctorId));
        treatment.setPatId(Integer.valueOf(patId));
        treatmentService.insName(treatment);

        return new Result(CodeStatus.OK,true,"保存成功");
    }

    @PostMapping("/update")
    @ResponseBody
    public Result update(HttpServletRequest request){

        String id = request.getParameter("id");

        regOfficeService.updateStatus(Integer.valueOf(id));

        return new Result(CodeStatus.OK,true,"删除成功");
    }


}
